#include "webapi_amap_base.h"

#include <HardwareSerial.h>
#include <string.h>

namespace webapi {

GeoLocation GetRectangleCenter(const GeoRectangle& rect) {
  GeoLocation center{};
  center.longitude = (rect.leftUp.longitude + rect.rightDown.longitude) / 2;
  center.latitude = (rect.leftUp.latitude + rect.rightDown.latitude) / 2;
  return center;
}

bool AmapWebServiceBase::SetAmapApiKey(const char* key) {
  int len = (int)strlen(key);
  if (len >= sizeof(this->_amapApiKey)) {
    Serial.printf("AMAP_API_KEY %d too long!\r\n", len);
    return false;
  }
  memcpy(this->_amapApiKey, key, len + 1);
  Serial.printf("AMAP_API_KEY %d setup OK!\r\n", len);
  return true;
}

const char* AmapWebServiceBase::GetAmapApiKey() const { return this->_amapApiKey; }

}  // namespace webapi
